<template>
  <div class="main">
    <!-- 添加加载动画容器 -->
    <div v-show="isLoading" class="loading-overlay">
      <div class="loading-spinner"></div>
    </div>
    <RouterView />
    <el-icon class="back-to-top" @click="scrollToTop" v-show="isShowScrollToTop">
      <Top />
    </el-icon>
  </div>
</template>

<script setup lang="ts">
  /**
   * 主体内容 一般组件
   * 用来显示主体内容
   * 路由跳转 中转站
   */
  import { RouterView } from 'vue-router'
  import { Top } from '@element-plus/icons-vue'
  import { inject, ref, provide, onMounted } from 'vue'

  const isShowScrollToTop = ref(true)
  const scrollToTop = inject('scrollToTop') as () => void

  // 加载状态
  const isLoading = ref(true)
  const handleScroll = (flag: boolean) => {
    isShowScrollToTop.value = flag
  }
  provide('handleScroll', handleScroll)

  onMounted(() => {
    // 添加1秒延迟的加载动画
    setTimeout(() => {
      isLoading.value = false
    }, 500)
  })
</script>

<style scoped>
  .main {
    position: relative;
  }

  .back-to-top {
    position: fixed;
    right: 40px;
    bottom: 40px;
    font-size: 54px;
    color: white;
    cursor: pointer;
    z-index: 100;
    background-color: rgb(75, 142, 220);
    border-radius: 50%;
    padding: 10px;
    transition: all 0.3s ease;
  }

  .back-to-top:hover {
    background-color: #ffdb46;
    color: #000000;
    transform: scale(1.1);
  }

  .loading-overlay {
    position: absolute;
    top: 0%;
    left: 0%;
    padding-left: 36%;
    padding-top: 10%;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 1);
    justify-content: center;
    align-items: center;
    z-index: 999;
  }

  .loading-spinner {
    width: 300px;
    height: 300px;
    border: 6px solid #f3f3f3;
    border-top: 6px solid #3498db;
    border-radius: 50%;
    animation: spin 0.4s linear infinite;
  }

  @keyframes spin {
    0% {
      transform: rotate(0deg);
    }
    100% {
      transform: rotate(360deg);
    }
  }
</style>
